package cn.demo.app.websocket;

import cn.ifloat.brick.sprofile.websocket.init.SendMessageKit;
import cn.ifloat.sprofile.websocket.common.ReceiveType;
import cn.ifloat.sprofile.websocket.common.WebSocketEvent;
import cn.ifloat.sprofile.websocket.common.WsMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @author: float 2023/7/28 14:11
 */
@Component
public class ExampleWebSocketEvent implements WebSocketEvent {
    //    @Autowired
//    private SendMessageKit kit;
    public static final Logger logger = LoggerFactory.getLogger(ExampleWebSocketEvent.class.getName());
    @Autowired
    private SendMessageKit messageKit;
    @Override
    public String name() {
        return "demo";
    }

    @Override
    public void init() {
        logger.info("example websocket event init");
    }

    @Override
    public void open(String s, String s1) {
        logger.info("example websocket event.open {}:{}", s, s1);
    }

    @Override
    public void message(WsMessage wsMessage) {
        logger.info("example websocket event message {}", wsMessage.toString());
//        Session session = SessionContext.get("A");;
//        SessionContext.sendAll("1", "dsadsadas");
//        SessionContext.getAll();
        try {
            messageKit.registerGroup("*", Arrays.asList("a","b","c"));
            messageKit.send(WsMessage.create("a","*","123","anything", ReceiveType.GROUP.getOrgin()));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void error(Throwable throwable, String s) {
        logger.info("example websocket event error {} {}", s, throwable.getMessage());
    }

    @Override
    public void close(String s) {
        logger.info("example websocket event close {}", s);
    }
}
